﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThinkGeo.MapSuite;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.WebEdition;
using maptest.Common;

namespace maptest
{
    public partial class _Default : System.Web.UI.Page
    {
        public static string connectString = ConfigurationManager.ConnectionStrings["MapConn"].ToString();
        private bool IsGenCache
        {
            get
            {
                string isGenCache = Request.QueryString["IsGenCache"] == null ? string.Empty : Request.QueryString["IsGenCache"];
                bool used = false;
                if (!string.IsNullOrEmpty(isGenCache) && bool.TryParse(isGenCache, out used))
                    return used;
                return used;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"));
                Map1.CurrentExtent = new RectangleShape(11574062.93759, 2568107.72308, 11577574.26150, 2568107.72308);
                Map1.MapUnit = GeographyUnit.Meter;
                Map1.MapTools.OverlaySwitcher.Enabled = false;

                GoogleOverlay mapgg4 = new GoogleOverlay();
                mapgg4.GoogleMapType = GoogleMapType.Normal;
                mapgg4.Name = "google";
                mapgg4.JavaScriptLibraryUri = new Uri(ConfigurationManager.AppSettings["GoogleUriV3"]);

                //Map1.CustomOverlays.Add(mapgg4);
                Map1.ZoomLevelSet = CustomZoomlevelSets();
                //doc tu sql ra
                LayerOverlay staticOverlay = new LayerOverlay("SQL", false, TileType.MultipleTile);
                staticOverlay.IsVisibleInOverlaySwitcher = false;
                sql data = new sql();
                DataTable dataTable = data.get("SELECT TABLE_NAME FROM information_schema.tables").Tables[0];
                foreach (DataRow row in dataTable.Rows)
                {
                    try
                    {
                        DataTable item = data.get("SELECT TOP 1 style FROM " + row[0]).Tables[0];
                        DataRow rowItem = item.Rows[0];
                        string layer = rowItem["style"].ToString();
                        if (!string.IsNullOrEmpty(layer))
                        {
                            GeoSerializer serializer = new GeoSerializer();
                            MsSql2008FeatureLayer deSerializedLayer = serializer.Deserialize(layer) as MsSql2008FeatureLayer;
                            staticOverlay.Layers.Add(row[0].ToString(), deSerializedLayer);
                        }
                    }
                    catch (Exception)
                    {

                        //throw;
                    }

                }
                LayerOverlay googleOverlay = new LayerOverlay("google", true, TileType.MultipleTile);
                GoogleMapsLayer googleLayer = new GoogleMapsLayer();
                googleLayer.MapType = GoogleMapsMapType.RoadMap;
                googleOverlay.Layers.Add("Google Road", googleLayer);
                googleOverlay.ServerCache.CacheDirectory = MapPath(@"~\googleCache");
                googleOverlay.ClientCache.CacheId = "clientGoogle";
                //staticOverlay.ServerCache.CacheDirectory=MapPath(@"~\Imagecache");
                //staticOverlay.ClientCache.CacheId = "client";
                if (IsGenCache)
                {
                    RectangleShape rectangle = staticOverlay.GetBoundingBox();
                    foreach (ZoomLevel zoomLevel in Map1.ZoomLevelSet.GetZoomLevels())
                    {
                        staticOverlay.GenerateCacheImages(zoomLevel, rectangle, Map1.MapUnit);
                    }
                }
                Map1.CustomOverlays.Add(staticOverlay);
                Map1.CustomOverlays.Add(googleOverlay);
            }
        }

        private ZoomLevelSet CustomZoomlevelSets()
        {
            ZoomLevelSet myZoomlevelset = new ZoomLevelSet();

            GoogleMapsZoomLevelSet googlezoomlevelsets = new GoogleMapsZoomLevelSet();
            foreach (ZoomLevel item in googlezoomlevelsets.GetZoomLevels().Skip(13).Take(7))
            {
                myZoomlevelset.CustomZoomLevels.Add(item);
            }

            return myZoomlevelset;
        }
    }
}